/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "CTrackView.h"
#include "CTrack.h"
#include "CPartView.h"
#include "CBar.h"
#include "CBarView.h"
#include "CGlobals.h"
#include "CResources.h"
#include "CDebug.h"
#include "CScore.h"
#include "CFraction.h"

#include <QGraphicsLineItem>
#include <QGraphicsPixmapItem>
#include <QPen>

using namespace easyscore::utils;
using namespace easyscore::globals;

namespace easyscore
{
    namespace core
    {
        //Accessors
        void CTrackView::setPartView ( CPartView* value )
        {
            mPartView = value;
            setParentItem ( value );
            showBorder(false);
        }

        //Consructors, Initialization and Destructors
        CTrackView::CTrackView ( CTrack *model )
        {
            mModel = model;
            mTimeNumeratorText = 0;
            mTimeDenominatorText = 0;
            mClefPixmap = 0;


            setZValue ( 2 );
        }

        void CTrackView::initialize()
        {
            //set initial size
            setRect ( rect().x(),rect().y(),CGlobals::getEmptyTrackWidth(),CGlobals::getTrackHeight() );

            //initialize clef pixmap
            mClefPixmap = new QGraphicsPixmapItem ( CResources::getInstance()->getClefPixmap ( mModel->getClef().getClef() ) );
            mClefPixmap->setParentItem ( this );
            mClefPixmap->setZValue ( 1 );
            mClefPixmap->setPos ( 300, 820 );

            CFraction time = mModel->getParentScore()->getTime();

            mTimeNumeratorText = new QGraphicsTextItem ( QString::number(time.getNumerator()));
            mTimeNumeratorText->setFont(QFont("Verdana", 170));
            mTimeNumeratorText->setParentItem ( this );
            mTimeNumeratorText->setZValue ( 1 );
            mTimeNumeratorText->setPos ( 360 + mClefPixmap->boundingRect().width(), 900 );

            mTimeDenominatorText = new QGraphicsTextItem ( QString::number(time.getDenominator()) );
            mTimeDenominatorText->setFont(QFont("Verdana", 170));
            mTimeDenominatorText->setParentItem ( this );
            mTimeDenominatorText->setZValue ( 1 );
            mTimeDenominatorText->setPos ( 360 + mClefPixmap->boundingRect().width(), 1100 );

            //create & initialize lines
            for ( int i = 0; i < CTrackView::mLINE_COUNT; ++i )
            {
                QGraphicsLineItem *line = new QGraphicsLineItem ( );
                line->setParentItem ( this );
                line->setZValue ( 3 );
                mLines[i] = line;
            }

            update();
        }

        void CTrackView::clear()
        {
            if ( mClefPixmap )
            {
                delete mClefPixmap;
                mClefPixmap = 0;
            }
        }

        CTrackView::~CTrackView()
        {
            clear();
        }

        void CTrackView::update()
        {
            int currentLeft = CGlobals::getTrackFirstBarLeft();
            foreach ( CBar *bar, mModel->getBars() )
            {
                if ( !bar->hasView() )
                {
                    bar->createView();
                    CBarView *barView = bar->getView();
                    barView->setParentItem ( this );
                    barView->initialize();
                }

                bar->getView()->setPos ( currentLeft,CGlobals::getTrackTopPadding() );

                if(bar->getActive())
                {
                    bar->getView()->setVisible(true);
                    currentLeft += bar->getView()->rect().width();
                }
                else
                {
                    bar->getView()->setVisible(false);
                }
            }


            //mAddBarControl.setPos ( currentLeft,CGlobals::getTrackTopPadding() );
            //currentLeft += mAddBarControl.rect().width();

            currentLeft += CGlobals::getTrackRightPadding();
            setRect ( rect().x(),rect().y(),currentLeft,rect().height() );

            //redraw lines
            int currentTop =  CGlobals::getBarTopPadding() + CGlobals::getFirstMainLineTop() ;
            for ( int i = 0; i < CTrackView::mLINE_COUNT; ++i )
            {
                int x1 = CGlobals::getTrackLeftPadding();
                int x2 = rect().width() - CGlobals::getTrackRightPadding();
                int y = currentTop;

                mLines[i]->setLine ( x1,y,x2,y );

                currentTop += CGlobals::getUnitSymbolWidth();
            }

            refreshLayout();
        }

        void CTrackView::refreshLayout()
        {
            mPartView->refreshLayout();
        }

        void CTrackView::addBar()
        {
            mModel->addBar();
        }

    }//namespace core
}//namespace easyscore


